package com.leedsoft.controller;

import com.leedsoft.common.AjaxReply;
import com.leedsoft.common.Page;
import com.leedsoft.controller.vo.PageVo;
import com.leedsoft.domain.AidOrg;
import com.leedsoft.service.AidOrgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/aid_org")
public class AidOrgController {
    private static final Logger logger = LoggerFactory.getLogger(AidController.class);

    @Autowired
    private AidOrgService aidOrgService;

    @RequestMapping("/list")
    public ModelAndView list(ModelAndView modelAndView, HttpServletRequest request){

        List<AidOrg> list = aidOrgService.queryList();//查询救援组织机构信息
        modelAndView.addObject("orgList",list);
        modelAndView.setViewName("aidOrg/aidOrg-list");
        return modelAndView;
    }

    @RequestMapping("dataGrid")
    @ResponseBody
    public Map dataGrid(PageVo vo){
        Page<AidOrg> page = aidOrgService.getOrgPlanList(vo);//查询救援组织机构信息//获取所有记录
        Map<String,Object> map = new HashMap();
        map.put("total",page.getCount());
        map.put("rows",page.getList());
        return map;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Map<String, Object> edit(AidOrg aidOrg){
        if(aidOrg.getId()==null){
            aidOrgService.add(aidOrg);
            String result = "ok";
            return AjaxReply.success(result,1);
        }else {
            aidOrg.setUpdate_time(new Date());
            aidOrgService.updateAidOrg(aidOrg);
            String result = "ok";
            return AjaxReply.success(result,1);
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(HttpServletRequest request){
        String id = request.getParameter("id");
        aidOrgService.deleteAidOrg(Long.parseLong(id));
        String result = "ok";
        return AjaxReply.success(result,1);
    }

}
